var reverseStr = function (s, k) {
    let n = s.length,
        l = 0,
        r = n - 1;
    let arr = s.split('')
    for (let i = 0; i < n; i++) {
        let c = ''
        if (k <= n - i && n - i <= 2 * k) {
            l = i;
            r = i + k - 1
            while (l < r) {
                c = arr[l]
                arr[l] = arr[r]
                arr[r] = c
                l++
                r--
            }
            break
        }
        if (n - i < k) {
            l = i
            r = n - 1
            while (l < r) {
                c = arr[l]
                arr[l] = arr[r]
                arr[r] = c
                l++
                r--
            }
            break
        }
       
        if (n - i > 2 * k) {
            l = i;
            r = i + k - 1
            while (l < r) {
                c = arr[l]
                arr[l] = arr[r]
                arr[r] = c
                l++
                r--
            }
            i = i + 2*k - 1
        }
    }

    return arr.join('')
};
console.log(reverseStr("abcdefg",2));